package com.example.jyapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jyapplication.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "regist";
    Button btnRegistConfirm,btnRegistBack;
    EditText etRegistName,etRegistPassword,etRegistRePassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }
    private void initView() {
        btnRegistConfirm = findViewById(R.id.btn_regist_confirm);
        btnRegistConfirm.setOnClickListener(this);
        btnRegistBack = findViewById(R.id.btn_regist_back);
        btnRegistBack.setOnClickListener(this);

        etRegistName = findViewById(R.id.et_regist_name);
        etRegistPassword = findViewById(R.id.et_regist_password);
        etRegistRePassword = findViewById(R.id.et_regist_repassword);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_regist_confirm:
                toRegist();

                break;
            case R.id.btn_regist_back:
                Intent intent2 = new Intent(this,LoginActivity.class);
                startActivity(intent2);
                break;
        }
    }

    private void toRegist() {
        final String name = etRegistName.getText().toString();
        final String password = etRegistPassword.getText().toString();
        final String repassword = etRegistRePassword.getText().toString();
        if (!name.equals("")&&
                !password.equals("")&&
                !repassword.equals("")){
            if (password.equals(repassword)){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient client = new OkHttpClient();
                        String url = "http://192.168.137.156:8080/18/All/user?telephone="+etRegistName.getText().toString();
                        FormBody body = new FormBody.Builder().build();
                        Request request = new Request.Builder()
                                .url(url)
                                .post(body)
                                .build();
                        final Call call = client.newCall(request);
                        try {
                            Response response = call.execute();
                            String data = "";
                            data = response.body().string();
                            Log.e(TAG, "run: "+data );
                            if (data.length()>4){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(RegistActivity.this, "该用户已存在！", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }else {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        OkHttpClient addClient = new OkHttpClient();
                                        FormBody body = new FormBody.Builder()
                                                .add("telephone",etRegistName.getText().toString())
                                                .add("password",etRegistRePassword.getText().toString())
                                                .build();
                                        String url = "http://10.20.246.109:8080/18/All/add";
                                        Request request = new Request.Builder()
                                                .url(url)
                                                .post(body)
                                                .build();
                                        Call addCall = addClient.newCall(request);
                                        try {
                                            Response addResponse = addCall.execute();
                                            final String data1 = addResponse.body().string();
                                            if (data1.equals("\""+"成功"+"\"")){
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Toast.makeText(RegistActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                                        Intent intent1 = new Intent(RegistActivity.this,FoodListActivity.class);
                                                        startActivity(intent1);
                                                    }
                                                });
                                            }else {
                                                Log.e(TAG, "run: "+data1 );
                                                Toast.makeText(RegistActivity.this, "注册失败！", Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }else{
                Toast.makeText(RegistActivity.this, " 俩次输入密码不一致！", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "toRegist: "+password+"..."+repassword );
            }
        }else {
            Toast.makeText(RegistActivity.this, "用户名或密码不能为空！", Toast.LENGTH_SHORT).show();
        }

    }
}